<?php

namespace Endor\EndorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Endor\EndorBundle\Entity\Articulos;
use Endor\EndorBundle\Form\ArticulosType;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Articulos controller.
 *
 */
class ArticulosController extends Controller {
    
    private static $destiny = "uploads/Articulos/";

    /**
     * Lists all Articulos entities.
     *
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('EndorBundle:Articulos')->findBy(array(
            'habilitado' => '1'
        ));
        
        return $this->render('EndorBundle:Articulos:index.html.twig', array(
                    'entities' => $entities
                ));
    }

    /**
     * Finds and displays a Articulos entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Articulos')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Articulos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        $entities = $em->getRepository("EndorBundle:Articulos")->find(array(
                'Kit' => $id));
        
        return $this->render('EndorBundle:Articulos:show.html.twig', array(
                    'entity' => $entity,
                    'entities' => $entities,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Articulos entity.
     *
     */
    public function newAction() {
        $entity = new Articulos();
        $form = $this->createForm(new ArticulosType(), $entity);
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository("EndorBundle:Articulos")->findAll();
        
        return $this->render('EndorBundle:Articulos:new.html.twig', array(
                    'entity' => $entity,
                    'entities' => $entities,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Articulos entity.
     *
     */
    public function createAction() {        
        $em = $this->getDoctrine()->getEntityManager();
        
        $entity = new Articulos();
        $request = $this->getRequest();
        $form = $this->createForm(new ArticulosType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $esPieza = $request->get("esPieza");
            
            if($esPieza != null){
                $articulo = $em->getRepository("EndorBundle:Articulos")->find($request->get("contenedor"));
                $entity->setKit($articulo);
            }else{
                $entity->setKit(null);
            }
            
            
            $uploadedFile = $form["imagen"];            
            
            if($uploadedFile->getData() != null){
                $fileName = $form->get("codigoBarra")->getClientData() . "." . $this->extension($uploadedFile->getData()->getClientOriginalName());
                $entity->setImagen($fileName);
            }
            
            $em->persist($entity);
            $em->flush();
            
            if($uploadedFile->getData() != null){
                $fileName = $entity->getId() . "." . $this->extension($uploadedFile->getData()->getClientOriginalName());
                $uploadedFile->getData()->move(self::$destiny, $fileName);

                $entity->setImagen($fileName);
            }
            
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('articulos'));
        }

        return $this->render('EndorBundle:Articulos:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }
    
    private function extension($file) {
        return substr(strrchr($file, '.'), 1);
    }

    /**
     * Displays a form to edit an existing Articulos entity.
     *
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Articulos')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Articulos entity.');
        }

        $editForm = $this->createForm(new ArticulosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $entities = $em->getRepository("EndorBundle:Articulos")->findAll();
        
        return $this->render('EndorBundle:Articulos:edit.html.twig', array(
            'entity' => $entity,        
            'entities' => $entities,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Edits an existing Articulos entity.
     *
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Articulos')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Articulos entity.');
        }
        
        $oldImage = $entity->getImagen();

        $editForm = $this->createForm(new ArticulosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $uploadedFile = $editForm["imagen"];
            
            if($uploadedFile->getData() != null){
                $fileName = $entity->getId() . "." . $this->extension($uploadedFile->getData()->getClientOriginalName());
                $uploadedFile->getData()->move(self::$destiny, $fileName);

                $entity->setImagen($fileName);
            }else{
                $entity->setImagen($oldImage);
            }
            
            $esPieza = $request->get("esPieza");
            
            if($esPieza != null){
                $articulo = $em->getRepository("EndorBundle:Articulos")->find($request->get("contenedor"));
                
                $entity->setKit($articulo);
            }else{
                $entity->setKit(null);
            }
            
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('articulos'));
        }

        return $this->render('EndorBundle:Articulos:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Deletes a Articulos entity.
     *
     */
    public function deleteAction($id) {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('EndorBundle:Articulos')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Articulos entity.');
            }

            $entity->setHabilitado(false);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('articulos'));
    }

    private function createDeleteForm($id) {
        return $this->createFormBuilder(array('id' => $id))
                        ->add('id', 'hidden')
                        ->getForm()
        ;
    }

}
